/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import it.polimi.se2.meteocal.control.CalendarManager;
import it.polimi.se2.meteocal.control.EventManager;
import it.polimi.se2.meteocal.control.LocationManager;
import it.polimi.se2.meteocal.control.ParticipationManager;
import it.polimi.se2.meteocal.control.UserManager;
import it.polimi.se2.meteocal.entity.Event;
import it.polimi.se2.meteocal.entity.Location;
import it.polimi.se2.meteocal.entity.Participation;
import it.polimi.se2.meteocal.entity.Users;
import java.sql.Time;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 *
 * @author Ugo
 */
@RunWith(Arquillian.class)
public class ParticipationManagerIT {
    
    public ParticipationManagerIT() {
    }

        
    @EJB
    CalendarManager calendarManager;
    @EJB
    EventManager eventManager;
    @EJB
    UserManager userManager;
    @EJB
    ParticipationManager participationManager;
    @EJB
    LocationManager locationManager;
   
    @PersistenceContext
    EntityManager entityManager;
   
    private final String email1 = "test@test.it";
    private final String email2 = "test2@test.it";
    private final String email3 = "test3@test.it";
   
    @Deployment
    public static WebArchive createArchiveAndDeploy() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(CalendarManager.class)
                .addClass(EventManager.class)
                .addClass(UserManager.class)
                .addClass(ParticipationManager.class)
                .addClass(LocationManager.class)
                .addPackage(Users.class.getPackage())
                .addPackage(Event.class.getPackage())
                .addPackage(Participation.class.getPackage())
                .addPackage(Location.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
     
    @Test
    public void UserManagerShouldBeInjected() {
        assertNotNull(userManager);
    }
    
    @Test
    public void EventManagerShouldBeInjected() {
        assertNotNull(eventManager);
    }
    
    @Test
    public void ParticipationManagerShouldBeInjected() {
        assertNotNull(participationManager);
    }
   
    @Test
    public void EntityManagerShouldBeInjected() {
        assertNotNull(entityManager);
    }
    
    @Test
    public void creationParticipation_getByEvent_getByEventUser_getByUser(){
        Users user1 = new Users();
        String password = "password";
        user1.setEmail(email1);
        user1.setName("Ugo");
        user1.setSurname("De Vos");
        user1.setPassword(password);
        user1.setCity("city");
        userManager.save(user1);
        
        Users user2 = new Users();
        user2.setEmail(email2);
        user2.setName("Emanuele");
        user2.setSurname("Casarini");
        user2.setPassword(password);
        user2.setCity("city");
        userManager.save(user2);
        
        Users user3 = new Users();
        user3.setEmail(email3);
        user3.setName("Daniele");
        user3.setSurname("Bartoli");
        user3.setPassword(password);
        user3.setCity("city");
        userManager.save(user3);
        
        Event event1 = new Event();
        event1.setCalendar(calendarManager.findCalendarByUser(user1));
        event1.setTitle("TITLE");
        event1.setData(new Date());
        event1.setBeginn(new Time(12, 0, 0));
        event1.setEndd(new Time(20, 0, 0));
        locationManager.create(new Location("Tokyo"));
        event1.setLocation(new Location("Tokyo"));
        event1.setPrivatee(false); //!!!!!!!!!!!!!!!
        eventManager.create(event1);
        
        Participation part1 = new Participation();
        part1.setEvent(event1);
        part1.setUser(user2);
        participationManager.create(part1);
        Participation part = participationManager.findParticipationByEventUser(event1, user2);
        
        assertNotNull(part);
        
        Event event2 = new Event();
        event2.setCalendar(calendarManager.findCalendarByUser(user1));
        event2.setTitle("TITLE");
        event2.setData(new Date());
        event2.setBeginn(new Time(20, 0, 0));
        event2.setEndd(new Time(00, 0, 0));
        locationManager.create(new Location("Berlino"));
        event2.setLocation(new Location("Berlino"));
        event2.setPrivatee(false); //!!!!!!!!!!!!!!!
        eventManager.create(event2);
        
        Participation part2 = new Participation();
        part2.setEvent(event1);
        part2.setUser(user3);
        participationManager.create(part2);
       
        
        Participation part3 = new Participation();
        part3.setEvent(event2);
        part3.setUser(user2);
        participationManager.create(part3);
        
        
        
        // al momento della creazione sono in PENDING
        //  il findparticipationbyuser cerca quelle che non sono in pending
        List<Participation> partuser2 = participationManager.findParticipationByUser(user2);
        assertEquals(0,partuser2.size());
        
        part1.setState(Participation.State.CONFIRMED);
        participationManager.edit(part1);
        
        partuser2 = participationManager.findParticipationByUser(user2);
        assertEquals(1,partuser2.size());
        
        List<Participation> partevent1 = participationManager.findParticipationByEvent(event1);
        assertEquals(partevent1.size(),2);
      
        
     }
}
